package com.oet.service.manager;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;

import org.apache.log4j.Logger;

public class StreamGobbler extends Thread {

	InputStream is;
    String type;
    File file;
    
    StreamGobbler(InputStream is, String type)
    {
        this(is, type, null);
    }
    StreamGobbler(InputStream is, String type, File redirect)
    {
        this.is = is;
        this.type = type;
        this.file = redirect;
    }
    
    public void run()
    {
    	PrintWriter pw = null;
    	BufferedReader br = null;
    	InputStreamReader isr = null;
        try
        {
            
            if (file != null)
                pw = new PrintWriter(file);
                
            isr = new InputStreamReader(is);
            br = new BufferedReader(isr);
            String line=null;
            while ( (line = br.readLine()) != null)
            {
                if (pw != null)
                    pw.println(line);
//                System.out.println(type + ">" + line);    
            }
            br.close();
            
        } catch (IOException ioe)
            {
            	ioe.printStackTrace();  
            }
        finally{
        	if (isr != null)
        	{
        		try {
					isr.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
        	}
        	if (br != null)
        	{
        		try {
					br.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
        	}
        	if (pw != null)
        	{
                pw.flush();
                pw.close();
        	}
        	
        }
    }
}
